package com.example.mapp.buildSql;

import com.example.entity.paperlessMaintance.PaperlessMaintanceApply;
import org.apache.commons.lang3.StringUtils;

public class PaperlessMaintMappSql {

    public String queryPaperlessMaint(PaperlessMaintanceApply paperlessMaintanceApply){
        StringBuilder builder = new StringBuilder("select * from paperless_maintance_apply where 1=1 ");
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getJgPartyId())){
            builder.append(" and jg_party_id = #{jgPartyId}");
        }
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getParentJgPartyId())){
            builder.append(" and parent_jg_party_id = #{parentJgPartyId}");
            builder.append(" and apply_status not in('1')");
        }
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getPartyName())){
            builder.append(" and party_name = #{partyName}");
        }
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getContacts())){
            builder.append(" and contacts = #{contacts}");
        }
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getApplyStatus())){
            builder.append(" and apply_status = #{applyStatus}");
        }
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getStartDate())){
            builder.append(" and apply_date > #{startDate}");
        }
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getEndDate())){
            builder.append(" and apply_date <= #{endDate}");
        }
        return builder.toString();
    }

    public String updatePaperlessMaint(PaperlessMaintanceApply paperlessMaintanceApply){
        StringBuilder builder = new StringBuilder("update paperless_maintance_apply set ");
        StringBuilder setValue = new StringBuilder();
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getApplyStatus())){
            setValue.append(" apply_status = #{applyStatus},");
        }

        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getRemarks())){
            setValue.append(" remarks = #{remarks},");
        }
        if(StringUtils.isNotEmpty(paperlessMaintanceApply.getApprovalDate())){
            setValue.append(" approval_date = #{approvalDate},");
        }
        builder.append(setValue.substring(0,setValue.lastIndexOf(","))).append(" where 1=1 ");
        if(paperlessMaintanceApply.getId() != null){
            builder.append(" and id = #{id}");
        }

        return builder.toString();
    }
}
